package cn.itcast.streaming.utils;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

import java.util.ArrayList;
import java.util.List;

public class MongoUtil {

    public static MongoClient getConnect() {
        String host = ConfigLoader.getProperty("mongo.host");
        int port = Integer.parseInt(ConfigLoader.getProperty("mongo.port"));
        String userName = ConfigLoader.getProperty("mongo.userName");
        String password = ConfigLoader.getProperty("mongo.password");
        String source = ConfigLoader.getProperty("mongo.source");
        ServerAddress serverAddress = new ServerAddress(host, port);
        List<MongoCredential> credential = new ArrayList<>();
        //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
        MongoCredential mongoCredential1 = MongoCredential.createScramSha1Credential(userName, source, password.toCharArray());
        credential.add(mongoCredential1);
        //通过连接认证获取MongoDB连接
        //MongoClient mongoClient = new MongoClient(serverAddress, credential);
        MongoClient mongoClient = new MongoClient(host,port);
        return mongoClient;
    }
}